#include <REGX52.H>
#include <intrins.h>

void Delay10ms(void)	//@12.000MHz
{
	unsigned char data i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

void Delay20ms(void)	//@12.000MHz
{
	unsigned char data i, j;

	i = 234;
	j = 115;
	do
	{
		while (--j);
	} while (--i);
}


int key_check_1()
{
	  int key;
		P1=0xFF;
		P1_0=0;
		if(P1_4==0){Delay20ms();while(P1_4==0);key=0;}
		if(P1_5==0){Delay20ms();while(P1_5==0);key=1;}
		if(P1_6==0){Delay20ms();while(P1_6==0);key=2;}
		if(P1_7==0){Delay20ms();while(P1_7==0);key=3;}
		
		P1=0xFF;
		P1_1=0;
		if(P1_4==0){Delay20ms();while(P1_4==0);key=4;}
		if(P1_5==0){Delay20ms();while(P1_5==0);key=5;}
		if(P1_6==0){Delay20ms();while(P1_6==0);key=6;}
		if(P1_7==0){Delay20ms();while(P1_7==0);key=7;}
		
		P1=0xFF;
		P1_2=0;
		if(P1_4==0){Delay20ms();while(P1_4==0);key=8;}
		if(P1_5==0){Delay20ms();while(P1_5==0);key=9;}
		if(P1_6==0){Delay20ms();while(P1_6==0);key=10;}
		if(P1_7==0){Delay20ms();while(P1_7==0);key=11;}
	
		P1=0xFF;
		P1_3=0;
		if(P1_4==0){Delay20ms();while(P1_4==0);key=12;}
		if(P1_5==0){Delay20ms();while(P1_5==0);key=13;}
		if(P1_6==0){Delay20ms();while(P1_6==0);key=14;}
		if(P1_7==0){Delay20ms();while(P1_7==0);key=15;}
		return key;
}
unsigned int key_check_2()
{
		    int A,B,key;
				P1=0xf0;
				Delay10ms();
				if(P1!=0xf0){A=P1;}
				P1=0x0f;
				B=P1;		
				key=A|B;
				return key;
}


